package com.htfyun.ft.fragment.factory;

import android.os.Bundle;
import android.os.Message;
import android.os.StatFs;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.common.SDCardInfo;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.utils.SystemPropertyUtil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class FragmentUnisoundMicTest extends FactoryBaseFragment implements View.OnClickListener {

    private TextView txtStatus;
    private Button btnStart;
    private static final int WAIT_SECOND = 5;//由于点击按钮会去打开声卡, 所以需要等待大概5秒才真正开启云知声录音测试
    private int waitCount;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_unisound_mic_test;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        txtStatus = (TextView) findViewById(R.id.txtStatus);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(this);

        setBtnPassVisibility(View.GONE);
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        txtStatus.setText("");
    }

    @Override
    protected void onInvisible() {
        super.onInvisible();

        startOrStopUnisoundMicTest(false);
        btnStart.setEnabled(true);

        getHandler().removeMessages(MSG_CHECK_RESULT);
        getHandler().removeMessages(MSG_FORCE_FINISH);
        getHandler().removeMessages(MSG_TICK_SEC);

        txtStatus.setText("");

    }

    private void updateStatus(String status) {
        String info = getString(R.string.unisound_mic_test_status, status);
        txtStatus.setText(info);
    }

    private void updateExtraStatus(int count) {
        String info = getString(R.string.unisound_mic_test_extra_status, count);
        txtStatus.setText(info);
    }

    @Override
    public void onClick(View v) {
        if (v == btnStart) {

            btnStart.setEnabled(false);


            waitCount = WAIT_SECOND;
            getHandler().sendEmptyMessage(MSG_TICK_SEC);

        }
    }

    private void checkStart() {

        if (waitCount <= 0) {
            updateStatus(getString(R.string.unisound_mic_test_status_testing));

            startOrStopUnisoundMicTest(true);
            getHandler().sendEmptyMessageDelayed(MSG_CHECK_RESULT, PERIOD_CHECK_RESULT);
            getHandler().sendEmptyMessageDelayed(MSG_FORCE_FINISH, DELAY_FORCE_FINISH);

        } else {

            updateExtraStatus(waitCount);

            getHandler().sendEmptyMessageDelayed(MSG_TICK_SEC, PERIOD_TICK_SEC);
        }

        waitCount--;

    }

    private void checkResult() {
        TestResultAttr attr = getTestResultAttr();
        switch (attr) {
            case running:
                updateStatus(getString(R.string.unisound_mic_test_status_testing));
                getHandler().sendEmptyMessageDelayed(MSG_CHECK_RESULT, PERIOD_CHECK_RESULT);
                break;
            case ok:
                updateStatus(getString(R.string.unisound_mic_test_status_ok));
                getHandler().removeMessages(MSG_FORCE_FINISH);
                finishMeWithPassed(true);
                break;
            case fail:
                updateStatus(getString(R.string.unisound_mic_test_status_fail));
                getHandler().removeMessages(MSG_FORCE_FINISH);
                finishMeWithPassed(false);
                break;
        }

    }

    ////////////////////////////////////////
    private static final int MSG_TICK_SEC = 9;
    private static final int PERIOD_TICK_SEC = 1000;
    private static final int MSG_CHECK_RESULT = 10;
    private static final int PERIOD_CHECK_RESULT = 1000;
    private static final int MSG_FORCE_FINISH = 11;
    private static final int DELAY_FORCE_FINISH = 30 * 1000;
    @Override
    protected void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_TICK_SEC:
                checkStart();
                break;
            case MSG_CHECK_RESULT:
                checkResult();
                break;
            case MSG_FORCE_FINISH:
                finishMeWithPassed(false);
                break;
        }
    }

    ////////////////////////////////////////
    public enum TestResultAttr {
        running("running"),
        ok("ok"),
        fail("fail")
        ;
        private final String result;

        TestResultAttr(String result) {
            this.result = result;
        }

        public String getResult() {
            return result;
        }
    }

    private static final String UNISOUND_MIC_TEST_PROP = "sys.unisound.mic_test.result";

    private String getResultForUnisoundMicTest() {
        return SystemPropertyUtil.getSystemProperty(UNISOUND_MIC_TEST_PROP);
    }

    private TestResultAttr getTestResultAttr() {
        TestResultAttr attr = TestResultAttr.running;

        String result = getResultForUnisoundMicTest();

        for (TestResultAttr resultAttr : TestResultAttr.values()) {
            if (result.equalsIgnoreCase(resultAttr.getResult())) {
                attr = resultAttr;
            }
        }

        return attr;
    }


    private void startOrStopUnisoundMicTest(boolean start) {
        final String value = "unisound_mic_test";
        final String propNameStart = "ctl.start";
        final String propNameStop = "ctl.stop";

        if (start) {
            SystemPropertyUtil.setSystemProperty(propNameStart, value);
            DebugLog.e("UnisoundMicTest: start Test");
        } else {
            SystemPropertyUtil.setSystemProperty(propNameStop, value);
            DebugLog.e("UnisoundMicTest: stop Test");
        }
    }
}
